<template>
    <div class="com-wrapper">
        <Select v-model="docInfo[0]"  @on-change="(value) => _onChange('hospital', value)" style="width:200px" placeholder="医院">
            <Option v-for="item in hospitalList" :value="item.id" :key="item.orgName">{{ item.orgName }}</Option>
        </Select>
        <Select v-model="docInfo[1]"  @on-change="(value) => _onOpenChange('department', value)" style="width:200px; margin: 0 5px" placeholder="科室">
            <Option v-for="item in departmentList" :value="item.id" :key="item.id">{{ item.dictName }}</Option>
        </Select>
        <Select v-model="docInfo[2]"  @on-change="(value) => _onOpenChange('doctor', value)" style="width:200px" placeholder="医生">
            <Option v-for="item in doctorList" :value="item.id" :key="item.id">{{ item.name }}</Option>
        </Select>
    </div>
</template>

<script>
import api from "@/api/commonApi";
export default {
    model: {
        prop: "docInfo",
        event: "change"
    },
    props: {
        docInfo: {
            type: Array,
            default: []
        },
        onChange: () => {}
    },
    data() {
        return {
            hospitalList: [],
            departmentList: [],
            doctorList: [],
        }
    },
    mounted() {
        this.findKbaoHospitalRegisterDtoPage();
    },
    methods: {
        _onChange(type, value) {
            console.log(type,value)
            if (!value && value !== 0) {
                this.departmentList = [];
                this.doctorList = [];
                return false;
            };
            let record = null;
            if (type == 'hospital') {
                this.docInfo[1] = null;
                this.docInfo[2] = null;
                record = this.hospitalList.filter(item => item.id === value)[0];
                record.dictValue = record.id;
                record.dictName = record.orgName;
                this.departmentList = [];
                this.doctorList = [];
            } else if (type == 'department') {
                this.docInfo[2] = null;
                record = this.departmentList.filter(item => item.id === value)[0];
                record.dictValue = record.id;
                record.dictName = record.dictName;
                this.doctorList = [];
            } else if (type === 'doctor') {
                record = this.doctorList.filter(item => item.id === value)[0];
                record.dictValue = record.id;
                record.dictName = record.name;
            }
            // this.onChange({type, dictValue: record.dictValue, dictName: record.dictName, record});
        },
        findKbaoHospitalRegisterDtoPage() {
            const reqObj = {
                pageSize: 9999,
                pageNum: 1
            };
            this.$axios.post(api.findKbaoHospitalRegisterDtoPage, reqObj).then(res => {
                if (res.data.code === 1) {
                    let ret = res.data.object;
                    this.hospitalList = ret;
                } else {
                    this.$Message.error(res.data.message);
                }
            });
        },
        getDepartmentList(hospitalId) {
            const reqObj ={
                hospitalId,
                pageSize: 9999,
                pageNo: 1,
            };
            this.$axios.post(api.deptPageList, reqObj).then(res => {
                if (res.data.code === 1) {
                    let ret = res.data.object;
                    this.departmentList = ret.records;
                } else {
                    this.$Message.error(res.data.message);
                }
            });
        },
        operatefindDoctorListByDeptAndHos(hospitalId, departmentId) {
            const reqObj = {
                hospitalId,
                departmentId,
                pageSize: 9999,
                pageNo: 1,
                type:1
            };
            this.$axios.post(api.getDoctorInfo, reqObj).then(res => {
                if (res.data.code === 1) {
                    let ret = res.data.object;
                    this.doctorList = ret.records;
                } else {
                    this.$Message.error(res.data.message);
                }
            });
        }
    },
    computed: {
        docInfoNew() {
            return JSON.parse(JSON.stringify(this.docInfo));
        }
    },
    watch: {
        'docInfoNew': {
            handler(n, o) {
                console.log(n,o)
                if (n[0] !== o[0] && n[0]) {
                    this.getDepartmentList(n[0]);
                } else if (n[1] !== o[1] && n[1]) {
                    this.operatefindDoctorListByDeptAndHos(n[0], n[1]);
                }
            },
            deep: true
        }
    }
}
</script>
<style scoped lang="less">
    .com-wrapper {
        display: flex;
        align-items: center;
    }
</style>
